package com.styytech.dadabao.test.ResideMenu;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;

import com.styytech.dadabao.widge.ResideMenu.ResideMenu;
import com.styytech.dadabao.widge.ResideMenu.ResideMenuItem;
import com.styytech.dadabao.R;

import java.util.ArrayList;
import java.util.List;


public class MenuActivity extends FragmentActivity implements
		View.OnClickListener {
	private ResideMenu resideMenu;
	private MenuActivity mContext;

	private ResideMenuItem itemHome;
	private ResideMenuItem itemProfile;
	private ResideMenuItem itemProfile1;
	private ResideMenuItem itemCalendar;
	private ResideMenuItem itemSettings;
	private ResideMenuItem itemSettings1;
	private ResideMenuItem itemSettings2;
	private ResideMenuItem itemSettings3;
	private ResideMenuItem itemSettings4;
	public List<View> itemList=new ArrayList<View>();
	public List<Integer> viewList=new ArrayList<Integer>();
	LinearLayout ssLayout;
	private final int photo_view=0;
	private final int itemHome_view=1;
	private final int itemProfile_view=2;
	private final int itemCalendar_view=3;
	private final int itemSettings_view=4;
	private final int itemProfile1_view=5;
	private TabHost mTabHost = null;
	private HomeFragment homeFragment;
	public Fragment profileFragment;
	public SettingsFragment settingsFragment;
	 private FragmentManager fragmentManager;
	 private int number=0;
	 private int number2=0;
	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_main);
		mContext = this;
		setUpMenu(); 
		fragmentManager = getSupportFragmentManager();  
		//homeFragment=new HomeFragment();
		//profileFragment=new ProfileFragment();
		//changeFragment(homeFragment);
	//	changeFragment(profileFragment,R.id.pro_fragment);
		//viewList.add(R.id.home_fragment);
		setOtherViewBackground(itemProfile);
		setSelection(1);
	}

	private void setUpMenu() {

		// attach to current activity;
		//LinearLayout fl=(LinearLayout)findViewById(R.id.main_fragment);
		resideMenu = new ResideMenu(this);
		resideMenu.setBackground(R.drawable.bg2);
		resideMenu.attachToActivity(this);
		//resideMenu.setMenuListener(menuListener);
		resideMenu.setScaleValue(0.6f);
		ssLayout=resideMenu.getPhotoView();
		// create menu items;
		itemHome = new ResideMenuItem(this, R.drawable.icon_profile, "Home");
		itemProfile = new ResideMenuItem(this, R.drawable.icon_profile,
				"Profile");
		itemProfile1 = new ResideMenuItem(this, R.drawable.icon_profile,
				"Profile1");
		itemCalendar = new ResideMenuItem(this, R.drawable.icon_calendar,
				"Calendar");
		itemSettings = new ResideMenuItem(this, R.drawable.icon_settings,
				"Settings");

		ssLayout.setId(photo_view);
		ssLayout.setOnClickListener(this);

		itemHome.setId(itemHome_view);
		itemHome.setOnClickListener(this);
		itemProfile.setId(itemProfile_view);
		itemProfile.setOnClickListener(this);
		itemProfile1.setId(itemProfile1_view);
		itemProfile1.setOnClickListener(this);
		itemCalendar.setId(itemCalendar_view);
		itemCalendar.setOnClickListener(this);
		itemSettings.setId(itemSettings_view);
		itemSettings.setOnClickListener(this);
		
		resideMenu.addMenuItem(itemHome);
		resideMenu.addMenuItem(itemProfile);
		resideMenu.addMenuItem(itemProfile1);
		resideMenu.addMenuItem(itemCalendar);
		resideMenu.addMenuItem(itemSettings);

		//itemProfile1.setVisibility(View.GONE);


	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		return resideMenu.dispatchTouchEvent(ev);
	}
	//点击菜单取消其他菜单背景色,并给当前点击菜单改变背景
	private void setOtherViewBackground(View view){
		if (itemList!=null) {
			for (int i = 0; i < itemList.size(); i++) {
				itemList.get(i).setBackgroundColor(getResources().getColor(android.R.color.transparent));
			}
			itemList.clear();
		}

		//view.setBackgroundColor(getResources().getColor(R.color.w1));
		view.setBackgroundDrawable(getResources().getDrawable(R.drawable.residemenu_onclick));
		itemList.add(view);
		
	}

	@Override
	public void onClick(View view) {
		Log.e("---", "view======================="+view);
		setOtherViewBackground(view);

	/*	if (number==0) {
			itemProfile.setVisibility(View.VISIBLE);
    		itemProfile1.setVisibility(View.GONE);
		}else {
			itemProfile.setVisibility(View.GONE);
    		itemProfile1.setVisibility(View.VISIBLE);
		}*/
		switch (view.getId()) {
		case photo_view:

			setSelection(0);
			resideMenu.closeMenu();
			break;
		case itemHome_view:	

			setSelection(0);
			resideMenu.closeMenu();	
			break;
		case itemProfile_view:
			setSelection(1);
			resideMenu.closeMenu();	

			break;

			case itemCalendar_view:
				setSelection(5);
				resideMenu.closeMenu();
				break;
		case itemProfile1_view:
			setSelection(5);
			resideMenu.closeMenu();	
			case itemSettings_view:
				setSelection(5);
				resideMenu.closeMenu();
				break;

		default:

			break;
		}
		if (view.getId()==photo_view) {
			if(itemList!=null) {
				for (int i = 0; i < itemList.size(); i++) {
					itemList.get(i).setBackgroundColor(getResources().getColor(android.R.color.transparent));
				}
				itemList.clear();
			}
		}else {

				setOtherViewBackground(view);
		}
	}


	private void setSelection(int index) {  
		resideMenu.clearIgnoredViewList();
	    // 每次选中之前先清楚掉上次的选中状态
	   // clearSelection();  
	    // 开启一个Fragment事务  
	    FragmentTransaction transaction = fragmentManager.beginTransaction();  
	    // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况  
	    hideFragments(transaction);  
	    switch (index) {  
	    case 0:  
	        if (homeFragment == null) {  
	            // 如果MessageFragment为空，则创建一个并添加到界面上  
	            homeFragment = new HomeFragment();  
	            transaction.add(R.id.main_fragment, homeFragment);  
	        } else {  
	            // 如果MessageFragment不为空，则直接将它显示出来
	            transaction.show(homeFragment);  
	            RelativeLayout ignored_view = (RelativeLayout)findViewById(R.id.home);
				 resideMenu.addIgnoredView(ignored_view);
	        }  
	      
	        break;  
	    case 1: 
	    		
	    		if (profileFragment == null) {  
	 	            // 如果ContactsFragment为空，则创建一个并添加到界面上  
	    			// number2=number;
	 	            profileFragment = new ProfileFragment();  
	 	            transaction.add(R.id.main_fragment, profileFragment);  
	 	        } else {  
	 	            // 如果ContactsFragment不为空，则直接将它显示出来  
	 	            transaction.show(profileFragment);  
	       
	 	        }
	    	break;
	    case 3:
	    	
	    	break;
	    case 4:
	    	break;
	    case 5:
	    	
	    	 if (settingsFragment == null) {  
	 	            // 如果ContactsFragment为空，则创建一个并添加到界面上  
				// number2=number;
				 settingsFragment = new SettingsFragment();  
	 	            transaction.add(R.id.main_fragment, settingsFragment);  
	 	        } else {  
	 	            // 如果ContactsFragment不为空，则直接将它显示出来  
	 	            transaction.show(settingsFragment);  
	 	        }  
	    	break;
	    } 
	    transaction.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
	    transaction.commit();  
	}  

	private void hideFragments(FragmentTransaction transaction) {  
	    if (homeFragment != null) {  
	        transaction.hide(homeFragment);  
	    }  
	    if (profileFragment != null) {  
	        transaction.hide(profileFragment);  
	    } 
	    if (settingsFragment != null) {  
	        transaction.hide(settingsFragment);  
	    }  
	}  

	// What good method is to access resideMenu
	public ResideMenu getResideMenu() {
		return resideMenu;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		itemList=null;
	}
	
}
